<template>
  <div class="loginBody" v-loading="loading" >
    <el-card class="login">
      <div class="top">
        <h1>生科计算机专业作业管理系统</h1>
      </div>
      <div class="form">
        <el-form :model="userInfo" :rules="rules" ref="form">
          <el-form-item prop="username">
            <el-input
              size="large"
              :prefix-icon="User"
              placeholder="请输入账号"
              clearable
              v-model="userInfo.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              size="large"
              placeholder="请输入密码"
              v-model="userInfo.password"
              show-password
              type="password"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              style="width: 100%"
              @click="userLogin"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
  <el-dialog
    v-model="isShow"
    title="提示"
    width="500"
    center
    :show-close="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <template #header>
      <h1 class="h1">请选择你的身份</h1>
    </template>
    <div class="body">
      <el-radio-group v-model="typeNumber">
        <el-radio :value="1" border>学生</el-radio>
        <el-radio :value="2" border>讲师</el-radio>
      </el-radio-group>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="isShow = false"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import useUserStore from "@/stores/modules/userStore";
import { ElMessage , ElNotification } from "element-plus";
import { useRouter } from "vue-router";
import { greetBasedOnTime } from '@/utiles/time';
import useSettingStore from "@/stores/modules/setting";
let settingStore = useSettingStore();
let router = useRouter();
let userStore = useUserStore();
let isShow = ref(true);
let loading = ref(false);
//表单组件
let form = ref(null);
//身份
let typeNumber = ref(1);
//用户信息
let userInfo = reactive({
  username: "",
  password: "",
});
//表单校验
let rules = reactive({
  username: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 2, max: 13, message: "账号长度在2-13之间", trigger: "change" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 3, max: 16, message: "密码长度在3-16之间", trigger: "change" },
  ],
});
//用户登录
const userLogin = async () => {
  await form.value.validate();
  let data = {
    name: userInfo.username,
    password: userInfo.password,
    status: typeNumber.value,
  };
 try {
  loading.value = true
  await  userStore.typeLogins(data)
  ElMessage.success('登陆成功')
  loading.value = false
  router.push('/')
  settingStore.getUserInfo()
  hello()
 } catch (error) {
  loading.value = false
 }
};
const hello = ()=>{
  ElNotification({
    title:`${greetBasedOnTime()},${userStore.userInfo['teaName']? userStore.userInfo['teaName']:userStore.userInfo['stuName']}!!!`,
    message: '欢迎使用湖北生物科技职业学院作业管理系统。',
    type: 'success'
  })
}
</script>

<style lang="scss" scoped>
:deep(.el-card) {
  background-color: #f3f6fd;
}
:deep(.el-input) {
  .el-input__wrapper {
    padding: 5px;
    background-color: transparent;
    box-shadow: none;

    &.is-focus,
    &:hover {
      box-shadow: none !important;
    }

    input:-webkit-autofill {
      /* 通过延时渲染背景色变相去除背景颜色 */
      transition: background-color 1000s ease-in-out 0s;
    }
  }
}
.loginBody {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/login-bg.jpg") no-repeat center right;
  display: flex;
  justify-content: center;
  align-items: center;

  .login {
    width: 400px;
    height: 400px;
    border-radius: 10px;
    display: flex;
    flex-direction: column;

    .el-form-item {
      background: var(--el-input-bg-color);
      border: 1px solid var(--el-border-color);
      border-radius: 5px;
      :deep(.el-input__prefix) {
        color: #000000;
      }
    }
    .top {
      height: 80px;
      text-align: center;
      line-height: 50px;
      font-size: 24px;
      color: #000000;
      font-weight: 900;
    }
  }
}
.body {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 0px;
}
.h1 {
  font-size: 20px;
}
</style>
